package com.base.self.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.base.exception.CustomException;
import com.base.mapper.voipmapper.SubscribersMapper;
import com.base.po.voip.Subscribers;
import com.base.result.ResponseResultUtils;
import com.base.self.entity.Ptintegral;
import com.base.self.entity.PtintegralRecharge;
import com.base.self.mapper.PtintegralMapper;
import com.base.self.mapper.PtintegralRechargeMapper;
import com.base.self.service.IPtintegralRechargeService;
import com.base.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service
public class PtintegralRechargeServiceImpl extends ServiceImpl<PtintegralRechargeMapper, PtintegralRecharge> implements IPtintegralRechargeService {

    @Autowired
    private PtintegralRechargeMapper ptintegralRechargeMapper;

    @Autowired
    private PtintegralMapper ptintegralMapper;

    @Autowired
    private SubscribersMapper subscribersMapper;

    @Override
    public Integer audit(String id, Integer status, String remake) {
   /*     if (2 == status && StringUtils.isEmpty(remake)){
           throw new CustomException("审核意见不能为空");
        }*/
        PtintegralRecharge ptintegralRecharge = ptintegralRechargeMapper.selectById(id);
        if (0 != ptintegralRecharge.getStatus()){
            throw new CustomException("已经审核过了!");
        }
        ptintegralRecharge.setStatus(status);
        ptintegralRecharge.setRemake(remake);
        ptintegralRecharge.setAuditTime(new Date());
        //审核通过增加积分
        if ( 1 == status){
            Ptintegral ptintegral = new Ptintegral();
            ptintegral.setStatus(1);
            ptintegral.setType(0);
            ptintegral.setSub(ptintegralRecharge.getSubId());
            ptintegral.setMoney(ptintegralRecharge.getMoney());
            ptintegral.setContext("充值");
            ptintegral.setCreateTime(DateUtil.date());
            ptintegralMapper.insert(ptintegral);
        }
        return ptintegralRechargeMapper.updateById(ptintegralRecharge);
    }

    @Override
    public Integer insert(PtintegralRecharge ptintegralRecharge) {
        Subscribers subscribers = subscribersMapper.selectByPrimaryKey(ptintegralRecharge.getSubId());
        ptintegralRecharge.setUsername(subscribers.getUsername());
        ptintegralRecharge.setMobile(subscribers.getMobile());
        ptintegralRecharge.setCreateTime(new Date());
        return ptintegralRechargeMapper.insert(ptintegralRecharge);
    }

}
